home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / drawer.zip / DRAWER.PAS < prev    next >
Pascal/Delphi Source File  |  1993-01-04  |  10KB  |  549 lines

  1. {$L-,D-}
  2. Program Drawer;
  3.  
  4. Uses
  5.     MSGraph, Shape, Canvas, CommWell,
  6.     ColorBar, Event, Dragger, Dialog, Crt;
  7.  
  8. const
  9.     MWIDTH            = 60;
  10.  
  11. type
  12.  
  13.     Handler = procedure( E : Event);
  14.  
  15. var
  16.     vc             : _VideoConfig;
  17.     cx, cy        : word;
  18.     CW             : CommWell;
  19.     FCW            : CommWell;
  20.     CB             : ColorBar;
  21.     Can         : Canvas;
  22.     MHandler     : Handler;
  23.     ColorShape     : GText;
  24.     Drag         : Dragger;
  25.     BDrag         : BDragger;
  26.     CurDrag        : Dragger;
  27.     GlobalState    : (Idling, Creating, Selecting, Moving, Sizing, Done);
  28.     GlobalShape    : (None, Rect, FRect, Ell, FEll, Lin, Txt);
  29.  
  30. {$F+}
  31. procedure HandleSelector( E : Event);
  32. var
  33.     x1, y1, x2, y2 : word;
  34.     dx, dy : integer;
  35. begin
  36.     if E.typ = LBUTTONDOWN then begin
  37.         CurDrag := BDrag;
  38.         if Can.PtInSelection(E.x, E.y) then begin
  39.             Can.GetRange( x1, y1, x2, y2);
  40.             if Can.OnHandle( E.x, E.y) then begin
  41.                 { we've got a size operation}
  42.                 GlobalState := Sizing;
  43.                 BDrag.Initialize( Size, x1, y1, x2, y2);
  44.                 end
  45.             else begin
  46.                 { we've got a move operation }
  47.                 GlobalState := Moving;
  48.                 BDrag.Initialize( Move, x1, y1, x2, y2);
  49.                 end;
  50.             end
  51.         else begin
  52.             GlobalState := Selecting;
  53.             Can.UnSelectAllObjects;
  54.             BDrag.Initialize( Size, E.x, E.y, E.x, E.y);
  55.             end;
  56.         CurDrag.StartDrag( E.x, E.y);
  57.         end
  58.     else if E.typ = LBUTTONUP then begin
  59.         BDrag.GetRange(x1, y1, x2, y2);
  60.         CurDrag.EndDrag( E.x, E.y );
  61.         if GlobalState=Moving then begin
  62.             dx := x2 - x1;
  63.             dy := y2 - y1;
  64.             Can.Move( dx, dy);
  65.             Can.Erase;
  66.             Can.Draw;
  67.             end
  68.         else if GlobalState=Sizing then begin
  69.             dx := x2 - x1;
  70.             dy := y2 - y1;
  71.             Can.Size( dx, dy );
  72.             Can.Erase;
  73.             Can.Draw;
  74.             end
  75.         else
  76.             { must be selecting }
  77.             Can.Lasso(x1, y1, x2, y2);
  78.         CurDrag := NIL;
  79.         GlobalState := Idling;
  80.         end
  81.     else if CurDrag<>NIL then CurDrag.Drag(E.x, E.y);
  82.  
  83. end;
  84.  
  85. procedure HandleQShapes( E : Event);
  86. var
  87.     r  : rectangle;
  88.     fr : FRectangle;
  89.     el : Ellipse;
  90.     fe : FEllipse;
  91.     s  : Shape;
  92.     x1, y1, x2, y2 : word;
  93. begin
  94.     if E.typ = LBUTTONDOWN then begin
  95.         GlobalState := Creating;
  96.         CurDrag := BDrag;
  97.         Can.UnSelectAllObjects;
  98.         BDrag.Initialize( Size, E.x, E.y, E.x, E.y);
  99.         CurDrag.StartDrag( E.x, E.y);
  100.         end
  101.     else if (E.typ = LBUTTONUP) then begin
  102.         GlobalState := Idling;
  103.         CurDrag.EndDrag( E.x, E.y );
  104.         BDrag.GetRange(x1, y1, x2, y2);
  105.         CurDrag := NIL;
  106.         case GlobalShape of
  107.             Rect : begin
  108.                     new(r);
  109.                     s := r;
  110.                    end;
  111.             FRect : begin
  112.                     new(fr);
  113.                     s := fr;
  114.                    end;
  115.             Ell : begin
  116.                     new(el);
  117.                     s := el;
  118.                    end;
  119.             FEll : begin
  120.                     new(fe);
  121.                     s := fe;
  122.                    end;
  123.             end;
  124.         s.Initialize( x1, y1, x2-x1, y2-y1, CB.GetColor);
  125.         if Can.AddShape(s) then begin
  126.             s.Draw;
  127.             Can.SelectObject(s)
  128.             end
  129.         else Dispose(s);
  130.         end
  131.     else if CurDrag<>NIL then CurDrag.Drag( E.x, E.y);
  132. end;
  133.  
  134. procedure HandleText( E : Event);
  135. var
  136.     T : GText;
  137.     P : Prompter;
  138.     x1, y1, x2, y2 : word;
  139. begin
  140.     if E.typ = LBUTTONDOWN then begin
  141.         GlobalState := Creating;
  142.         CurDrag := BDrag;
  143.         Can.UnSelectAllObjects;
  144.         BDrag.Initialize( Size, E.x, E.y, E.x, E.y);
  145.         CurDrag.StartDrag( E.x, E.y);
  146.         end
  147.     else if (E.typ = LBUTTONUP) then begin
  148.         GlobalState := Idling;
  149.         CurDrag.EndDrag( E.x, E.y );
  150.         BDrag.GetRange(x1, y1, x2, y2);
  151.         CurDrag := NIL;
  152.         new(P);
  153.         P.Initialize( 5, 15, 50,'Text:');
  154.         if P.Process then begin
  155.             new(T);
  156.             T.Initialize( x1, y1, x2-x1, y2-y1, CB.GetColor);
  157.             T.SetText(P.Response);
  158.             if Can.AddShape(T) then begin
  159.                 T.Draw;
  160.                 Can.SelectObject(T);
  161.                 end
  162.             else
  163.                 Dispose(T);
  164.             end;
  165.         Dispose(P);
  166.         end
  167.     else if CurDrag<>NIL then CurDrag.Drag( E.x, E.y);
  168. end;
  169.  
  170. procedure HandleLine( E : Event);
  171. var
  172.     l  : Line;
  173.     x1, y1, x2, y2 : word;
  174. begin
  175.     if E.typ = LBUTTONDOWN then begin
  176.         GlobalState := Creating;
  177.         Can.UnSelectAllObjects;
  178.         CurDrag := Drag;
  179.         CurDrag.StartDrag( E.x, E.y);
  180.         end
  181.     else if (E.typ = LBUTTONUP) then begin
  182.         GlobalState := Idling;
  183.         CurDrag.EndDrag( E.x, E.y );
  184.         Drag.GetRange(x1, y1, x2, y2);
  185.         CurDrag := NIL;
  186.         new(l);
  187.         l.Initialize( x1, y1, x2-x1, y2-y1, CB.GetColor);
  188.         if Can.AddShape(l) then begin
  189.             l.Draw;
  190.             Can.SelectObject(l);
  191.             end
  192.         else
  193.             Dispose(l);
  194.         end
  195.     else if CurDrag<>NIL then CurDrag.Drag( E.x, E.y);
  196. end;
  197.  
  198. procedure ChoseSelector;
  199. begin
  200.     GlobalShape := None;
  201.     MHandler := HandleSelector;
  202. end;
  203.  
  204. procedure ChoseRectangle;
  205. begin
  206.     GlobalShape := Rect;
  207.     MHandler := HandleQShapes;
  208. end;
  209.  
  210. procedure ChoseFRectangle;
  211. begin
  212.     GlobalShape := FRect;
  213.     MHandler := HandleQShapes;
  214. end;
  215.  
  216. procedure ChoseEllipse;
  217. begin
  218.     GlobalShape := Ell;
  219.     MHandler := HandleQShapes;
  220. end;
  221.  
  222. procedure ChoseFEllipse;
  223. begin
  224.     GlobalShape := FEll;
  225.     MHandler := HandleQShapes;
  226. end;
  227.  
  228. procedure ChoseLine;
  229. begin
  230.     GlobalShape := Lin;
  231.     MHandler := HandleLine;
  232. end;
  233.  
  234. procedure ChoseText;
  235. begin
  236.     GlobalShape := Txt;
  237.     MHandler := HandleText;
  238. end;
  239.  
  240. procedure ChoseColors;
  241. var
  242.     E : Event;
  243. begin
  244.     GlobalShape := None;
  245.     CW.SelectItem(1);
  246.     ChoseSelector;
  247.     CW.Erase;
  248.     CB.Draw;
  249.     ShowPointer;
  250.     while TRUE do begin
  251.         GetEvent(E);
  252.         if (E.typ = LBUTTONUP) and
  253.             CB.PtInRegion( E.x, E.y) then begin
  254.                 CB.Process( E.x, E.y);
  255.                 HidePointer;
  256.                 if Can.SelectedObject(NIL)=NIL then
  257.                     ColorShape.color := CB.GetColor
  258.                 else begin
  259.                     Can.ChangeColor( CB.GetColor );
  260.                     Can.Erase;
  261.                     Can.Draw;
  262.                     end;
  263.                 CB.Erase;
  264.                 CW.Draw;
  265.                 exit;
  266.                 end;
  267.         end;
  268. end;
  269.  
  270. procedure ChoseDelete;
  271. begin
  272.     GlobalShape := None;
  273.     Can.Delete;
  274.     Can.Erase;
  275.     Can.Draw;
  276.     CW.SelectItem(1);
  277.     ChoseSelector;
  278. end;
  279.  
  280. procedure ChoseCopy;
  281. begin
  282.     GlobalShape := None;
  283.     Can.Copy;
  284.     Can.Erase;
  285.     Can.Draw;
  286.     CW.SelectItem(1);
  287.     ChoseSelector;
  288. end;
  289.  
  290. procedure ChoseRedraw;
  291. begin
  292.     GlobalShape := None;
  293.     Can.Erase;
  294.     Can.Draw;
  295.     CW.SelectItem(1);
  296.     ChoseSelector;
  297. end;
  298.  
  299. procedure ChoseFile;
  300. var
  301.     E : Event;
  302. begin
  303.     GlobalShape := None;
  304.     CW.SelectItem(1);
  305.     ChoseSelector;
  306.     CW.Erase;
  307.     FCW.Draw;
  308.     ShowPointer;
  309.     while TRUE do begin
  310.         GetEvent(E);
  311.         HidePointer;
  312.         { Check if menu item.  If so, let file command well do it }
  313.         if (E.typ=LBUTTONDOWN) and FCW.PtInRegion( E.x, E.y) then begin
  314.                 repeat GetEvent(E) until E.typ=LBUTTONUP;
  315.                 if FCW.PtInRegion( E.x, E.y) then FCW.Process( E.x, E.y);
  316.                 { now get out }
  317.                 FCW.SelectItem(0);
  318.                 FCW.Erase;
  319.                 CW.Draw;
  320.                 exit;
  321.                 end;
  322.         ShowPointer;
  323.         end;
  324. end;
  325.  
  326. procedure ChoseQuit;
  327. begin
  328.     GlobalShape := None;
  329.     GlobalState := Done;
  330.     CW.SelectItem(1);
  331.     ChoseSelector;
  332. end;
  333.  
  334. function GetFileName( var fn : string) : boolean;
  335. var
  336.     p : Prompter;
  337. begin
  338.     new(p);
  339.     p.Initialize( 5, 15, 40, 'Filename:');
  340.     GetFileName := p.Process;
  341.     fn := p.Response;
  342. end;
  343.  
  344. procedure ChoseFNew;
  345. begin
  346.     Can.SelectAllObjects;
  347.     Can.Delete;
  348.     Can.Erase;
  349. {    Can.Draw; }
  350. end;
  351.  
  352. procedure ChoseFOpen;
  353. var
  354.     fn : string;
  355. begin
  356.     if GetFileName(fn) then Can.Load(fn);
  357. end;
  358.  
  359. procedure ChoseFSave;
  360. var
  361.     fn : string;
  362. begin
  363.     if GetFileName(fn) then Can.Save(fn);
  364. end;
  365.  
  366. procedure ChoseFCancel;
  367. begin
  368. end;
  369.  
  370. {$F-}
  371.  
  372. procedure CreateCommWell;
  373. const
  374.     NUMCOMMANDS = 13;
  375. var
  376.     s  : Selector;
  377.     r  : Rectangle;
  378.     fr : FRectangle;
  379.     e  : Ellipse;
  380.     fe : FEllipse;
  381.     l  : Line;
  382.     t  : GText;
  383. begin
  384.     new(CW);
  385.     CW.Initialize( (vc.numypixels-1) div numcommands, MWIDTH, vc.numcolors-1);
  386.  
  387.     new(s);
  388.     CW.AddCommand( s, ChoseSelector);
  389.  
  390.     new(r);
  391.     CW.AddCommand( r, ChoseRectangle);
  392.  
  393.     new(fr);
  394.     CW.AddCommand( fr, ChoseFRectangle);
  395.  
  396.     new(e);
  397.     CW.AddCommand( e, ChoseEllipse);
  398.  
  399.     new(fe);
  400.     CW.AddCommand( fe, ChoseFEllipse);
  401.  
  402.     new(l);
  403.     CW.AddCommand( l, ChoseLine);
  404.  
  405.     new(t);
  406.     CW.AddCommand( t, ChoseText );
  407.     t.SetText('Text');
  408.     t.SetHeight( cy );
  409.  
  410.     new(ColorShape);
  411.     CW.AddCommand( ColorShape, ChoseColors );
  412.     ColorShape.SetText('Colors...');
  413.     ColorShape.SetHeight( cy );
  414.  
  415.     new(t);
  416.     CW.AddCommand( t, ChoseDelete );
  417.     t.SetText('Delete');
  418.     t.SetHeight( cy );
  419.  
  420.     new(t);
  421.     CW.AddCommand( t, ChoseCopy );
  422.     t.SetText('Copy');
  423.     t.SetHeight( cy );
  424.  
  425.     new(t);
  426.     CW.AddCommand( t, ChoseRedraw );
  427.     t.SetText('Redraw');
  428.     t.SetHeight( cy );
  429.  
  430.     new(t);
  431.     CW.AddCommand( t, ChoseFile);
  432.     t.SetText('File...');
  433.     t.SetHeight( cy );
  434.  
  435.     new(t);
  436.     CW.AddCommand( t, ChoseQuit);
  437.     t.SetText('Quit');
  438.     t.SetHeight( cy );
  439.  
  440.     CW.Draw;
  441.     CW.SelectItem(1);
  442.     CW.Menu[1].DoIt;
  443. end;
  444.  
  445.  
  446. procedure CreateFCommWell;
  447. const
  448.     NUMCOMMANDS = 4;
  449. var
  450.     t  : GText;
  451. begin
  452.     new(FCW);
  453.     FCW.Initialize( (vc.numypixels-1) div numcommands, MWIDTH, vc.numcolors-1);
  454.  
  455.     new(t);
  456.     FCW.AddCommand( t, ChoseFNew );
  457.     t.SetText('New');
  458.     t.SetHeight( cy );
  459.  
  460.     new(t);
  461.     FCW.AddCommand( t, ChoseFOpen );
  462.     t.SetText('Open...');
  463.     t.SetHeight( cy );
  464.  
  465.     new(t);
  466.     FCW.AddCommand( t, ChoseFSave );
  467.     t.SetText('Save...');
  468.     t.SetHeight( cy );
  469.  
  470.     new(t);
  471.     FCW.AddCommand( t, ChoseFCancel);
  472.     t.SetText('Cancel');
  473.     t.SetHeight( cy );
  474. end;
  475.  
  476. procedure Initialize;
  477. var
  478.     vidrows : Integer;
  479.     numfonts : integer;
  480. begin
  481.     { initialize CRT unit }
  482.     DirectVideo := FALSE;
  483.  
  484.     { initialize MSGraph unit }
  485.     vidrows := _SetVideoMode( _ERESCOLOR );
  486.     numfonts := _RegisterFonts( '*.fon');
  487.     _GetVideoConfig( vc );
  488.  
  489.     { initialize screen dependent parameters }
  490.     cx := vc.numxpixels div 80;
  491.     cy := vc.numypixels div 25;
  492.  
  493.     { initialize the event processor }
  494.     EnableEvents;
  495.  
  496.     { initialize canvas }
  497.     new(Can);
  498.     Can.Initialize( MWIDTH+1, 0, vc.numxpixels-1, vc.numypixels-1);
  499.  
  500.     { initialize command wells }
  501.     CreateCommWell;
  502.     CreateFCommWell;
  503.  
  504.     { initialize color bar }
  505.     new(CB);
  506.     CB.Initialize( (vc.numypixels-1) div vc.numcolors, MWIDTH, vc.numcolors);
  507.  
  508.     { Create Dragger objects }
  509.     new(Drag);
  510.     new(BDrag);
  511.  
  512. end;
  513.  
  514. procedure Finalize;
  515. var
  516.     vidrows : Integer;
  517. begin
  518.     DisableEvents;
  519.     vidrows := _SetVideoMode( _DEFAULTMODE );
  520. end;
  521.  
  522. procedure ProcessEvents;
  523. label
  524.     DoneWithEvent;
  525. var
  526.     E : Event;
  527. begin
  528.     CurDrag := NIL;
  529.     GlobalState := Idling;
  530.     while GlobalState<>Done do begin
  531.         GetEvent(E);
  532.         HidePointer;
  533.         { Check if menu item.  If so, let command well do it }
  534.         if (E.typ=LBUTTONDOWN) and CW.PtInRegion( E.x, E.y) then begin
  535.                 repeat GetEvent(E) until E.typ=LBUTTONUP;
  536.                 if CW.PtInRegion( E.x, E.y) then CW.Process( E.x, E.y);
  537.                 end
  538.         else    MHandler( E );
  539.         ShowPointer;
  540.         end;    { while State<>done }
  541.  
  542. end;
  543.  
  544. begin
  545.     Initialize;
  546.     ProcessEvents;
  547.     Finalize;
  548. end.
  549.